package com.czq.gulimail.gulimailproduct.controller;

import com.czq.common.utils.R;
import com.czq.gulimail.gulimailproduct.entity.CategoryEntity;
import com.czq.gulimail.gulimailproduct.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;



/**
 * 商品三级分类
 *
 * @author czq
 * @email 1807099124@qq.com
 * @date 2024-09-23 19:03:06
 */
@RestController
@RequestMapping("gulimailproduct/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 多层分类树形查询
     */
    @RequestMapping("/list/tree")
    public R list(){
        List<CategoryEntity> res= categoryService.listWithTree();
        return R.ok().put("data", res);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{catId}")
    public R info(@PathVariable("catId") Long catId){
		CategoryEntity category = categoryService.getById(catId);

        return R.ok().put("category", category);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody CategoryEntity category){
		categoryService.save(category);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody CategoryEntity category){
        categoryService.updateCategoryAndRelations(category);
        return R.ok();
    }

    /**
     * 拖拽分类节点修改分类排序和层级
     */
    @RequestMapping("/updateByDragging")
    public R updateByDragging(@RequestBody List<CategoryEntity> category){
        for(CategoryEntity categoryEntity : category){
            categoryService.updateById(categoryEntity);
        }
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] catIds){
		categoryService.removeByIds(Arrays.asList(catIds));
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/deleteByIds")
    public R deleteByIds(@RequestBody Long[] catIds){
        categoryService.deleteCategoryAndRelations(catIds);
        return R.ok();
    }

}
